<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use Mail;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests\HomeRegisterPostRequest;
use App\Http\Requests;

use App\Http\Controllers\Controller;

class RegisterController extends Controller
{
	public function register()
    {
        //显示注册页
        return view('home.login.register');
    }
    /*
        执行注册
    */
    public function doregister(HomeRegisterPostRequest $request)
    {
        // dd($request->all());
        //session中提取vcode
        //检测验证码是否正确
        if($request->input('vcode') != session('code')){
            return back()->with('error','验证码不正确');
        }
        // die('验证码正确');
        //处理数据
        $data = $request->only(['username','password','email',]);
        //密码加密

        $data['password'] = Hash::make($data['password']);
        $data['token'] = str_random(50);
        $data['status'] = 1;//状态1:未激活 2:激活
        // dd($data);
        $id = DB::table('user')->insertGetId($data);
        // dd($id);
        //执行数据插入
        if($id){
            //注册成功后 发邮件
            // echo $id;
            $this->sendMail($id,$data['token'],$data['email']);
            return view('home.login.success');
        }else{
            return back()->with('error','注册失败');
        }
    }
  	//验证码
   	public function vcode(){
   		ob_clean();//清除输出缓存区的内容
        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 75, $height = 28, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        session(['Vcode'=>$phrase]);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();

       //看到图片输出 需要die
        // die;

   	}
    

    //邮件发送测试
    public function sendMail($id,$token,$email)
    {
        
       
        Mail::send('mail.re', ['id'=>$id,'token'=>$token], function ($message)use($email) {
            $message->to($email)->subject('激活邮件');
        });
        // return view('mail.re');
    }
   public function jihuo(Request $request){
        // dd($request->all());
        $id = $request->input('id');
        //根据id查询用户
        $user = DB::table('user')->where('id',$id)->first();
        //请求是否正确
        if($user->token == $request->input('token')){
            $data['status'] = 2;
            $data['token'] = str_random(50);
            $res = DB::table('user')->where('id',$id)->update($data);
            if($res){
                echo '激活成功';
                return redirect('/login');
            }else{
                echo '激活失败';
            }
        }else{
            echo '链接失效';
        }
    }
    public function getAjax(Request $request){
        $res = DB::table('user')->where('username',$request->input('name'))->first();
        //判断
        if($res){
            echo 0;
        }else{
            echo 1;
        }
    }
    
    public function getAjaxa(Request $request){
        $res = DB::table('user')->where('email',$request->input('name'))->first();
        //判断
        if($res){
            echo 0;
        }else{
            echo 1;
        }
    }

    //短信验证码
    public function sendMessage(Request $request)
    {
        $to = $request->input('phone');
        //初始化必填
        $options['accountsid']='ebee469a67a336df3f8d424e6411566d';
        $options['token']='3bc45e2468b8226fb2c024ad8d564259';


        //初始化 $options必填
        $ucpass = new \Ucpaas($options);
        //开发者账号信息查询默认为json或xml

        // echo $ucpass->getDevinfo('xml');

        //语音验证码,云之讯融合通讯开放平台收到请求后，向对象电话终端发起呼叫，接通电话后将播放指定语音验证码序列
        $appId = "33b79cea8a5b42d98e08c3a8a02d7ccf";
        $verifyCode = rand(1000,9999);
        //存session
        session(['code'=>$verifyCode]);
        // $to = "18701582426";

        $res = $ucpass->voiceCode($appId,$verifyCode,$to);
        //将json的数据转化为数组或对象
        $arr = json_decode($res,true);
        // dd($arr);
        return $arr['resp']['respCode'];

        //短信验证码（模板短信）,默认以65个汉字（同65个英文）为一条（可容纳字数受您应用名称占用字符影响），超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
        // $appId = "33b79cea8a5b42d98e08c3a8a02d7ccf";
        // $to = "18612345678";
        // $templateId = "1";
        // $param="test,1256,3";

        // echo $ucpass->templateSMS($appId,$to,$templateId,$param);
    }
}  
